﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimulationPresenter.Simulation.Helpers;
using Vector3=Microsoft.DirectX.Vector3;

namespace SimulationPresenter.Theater.Joints
{
    internal class SphericalJoint : IJoint
    {
        private Simulation.Primitive.Joints.SphericalJoint simulation;
        
        public IActorPropertiesProvider Actor1
        {
            get;
            set;
        }

        public IActorPropertiesProvider Actor2
        {
            get;
            set;
        }

        private Vector3 joinPoint;

        public Vector3 JoinPoint
        {
            get { return joinPoint; }
            set { joinPoint = value; }
        }

        internal IJoint CreateJoin(Simulation.SimulationEnvironment environment)
        {
            if ((null == Actor1) || !(Actor1 is RidigActorPrimitive))
            {
                throw new InvalidOperationException("Actor nr. 1 can not be converted to ActorPrimitive");
            }
            if ((null == Actor2) || !(Actor2 is RidigActorPrimitive))
            {
                throw new InvalidOperationException("Actor nr. 2 can not be converted to ActorPrimitive");
            }

            JoinPoint = Actor1.Position.CalculateMidPoint(Actor2.Position);

            simulation = new Simulation.Primitive.Joints.SphericalJoint()
                             {
                                 Actor1 = (Actor1 as RidigActorPrimitive).State,
                                 Actor2 = (Actor2 as RidigActorPrimitive).State,
                                 Position = JoinPoint,
                                 Name = Actor1.Name + "_JOIN_" + Actor2.Name
                             };

            simulation.PutOrUpdate(environment);

            return this;
        }
    }
}
